﻿using System;
using System.Diagnostics;
using System.Reflection;

namespace CSUploadOSUI
{
    [Serializable]
    public class CsUploadTool
    {
        internal string Path { get; set; }

        [NonSerialized] 
        public OutputPipeLine Newline;

        private readonly LineCommandProcess _process;

        public CsUploadTool()
        {
//            if (global::SQLMetalOSUICtrl.Properties.Settings.Default.SQLMetalPath.Length == 0)
                Path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) +
                    @"\Windows Azure SDK\v1.4\bin\csupload.exe";
//            else
//                Path = global::SQLMetalOSUICtrl.Properties.Settings.Default.SQLMetalPath;

            _process = new LineCommandProcess(this,create);
        }

        private void create(object sender, DataReceivedEventArgs e)
        {
            Newline(e.Data);
        }

        public void GenerateDBML(string inputFile, string outputFile)
        {
            Newline(
                _process.ExecuteWithParameters(
                    string.Format("/dbml:\"{0}\" \"{1}\"", outputFile, inputFile)));
        }


        public void GenerateCode(string inputFile,
            string outputFile, string namespaceAssigned)
        {
            Newline(
                _process.ExecuteWithParameters(
                    string.Format("/code:\"{0}\" \"{1}\" /namespace:\"{2}\"",
                    outputFile, inputFile, namespaceAssigned)));

        }

        public void GenerateMap(string inputFile, string outputMapFile, string outputCodeFile, string namespaceAssigned)
        {
            Newline(
                _process.ExecuteWithParameters(
                    string.Format("/map:\"{0}\" /code:\"{1}\" \"{2}\" /namespace:\"{3}\"",
                    outputMapFile, outputCodeFile, inputFile, namespaceAssigned)));
        }

        public void GetHelp()
        {
            Newline(_process.ExecuteWithNoParameters());
        }

        public void GetSqlMetalInformation()
        {
            Newline(_process.ExecuteForInformationPruposes());
        }

        internal void GetStatus()
        {
            Newline(string.Format("CSUpload Open Source User Interface " +
                "v.{0} \r\n" +
                "started succesfully at {1}",
                Version, DateTime.Now.ToShortTimeString()));
        }

        public string Version
        {
            get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
        }

        public void GetConnection()
        {
            _process.ExecuteAsyncWithParameters("Get-Connection");

        }

        public void SetConnection(string subscriptionId, string certificateThumb)
        {
            _process.ExecuteAsyncWithParameters(
                string.Format("Set-Connection SubscriptionId={0};CertificateThumbprint={1}"
                              , subscriptionId, certificateThumb));
        }

        public void ClearConnection()
        {
            _process.ExecuteAsyncWithParameters("Clear-Connection");
        }

        public void AddMRW()
        {
            //Add-VMImage -Connection "SubscriptionId=571ad9f0-689b-4eec-a83c-30b88310234f; CertificateThumbprint=F27F4186CE9C5B2AFD1E30FB54B59F8880C89C62" -Description "Base image Windows Server 2008 R2" -LiteralPath "C:\t\VMRole.vhd" -Name VMRole.vhd -Location "North Europe"
            _process.ExecuteAsyncWithParameters(
                "Add-VMImage -Connection \"SubscriptionId=571ad9f0-689b-4eec-a83c-30b88310234f; CertificateThumbprint=F27F4186CE9C5B2AFD1E30FB54B59F8880C89C62\" -Description \"Base image Windows Server 2008 R2\" -LiteralPath \"C:\\t\\VMRole.vhd\" -Name VMRole.vhd -Location \"North Europe\"");

        }

        public void Test()
        {
            _process.ExecuteAsyncWithParameters(" 192.168.1.1 -t");
        }

        public void GetAffinity()
        {
            _process.ExecuteAsyncWithParameters("Get-AffinityGroup");
        }

        public void GetLocation()
        {
            _process.ExecuteAsyncWithParameters("Get-Location");
        }

        public void GetVmImage()
        {
            _process.ExecuteAsyncWithParameters("Get-VMImage");
        }
    }

}
